package com.example.userdemo.controller;

import com.alibaba.excel.EasyExcel;
import com.example.userdemo.entity.User;
import com.example.userdemo.mapper.UserMapper;
import com.example.userdemo.service.UserService;
import com.example.userdemo.util.ErrorCode;
import com.example.userdemo.util.Response;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(path = "/add",method = RequestMethod.POST)
    public Response<String> addUser( String name, int sex, String description){
        int addRes = userService.addUser(name, sex, description);
        if (addRes > 0){
            return Response.success("新增用户成功");
        }else {
            return Response.failure(ErrorCode.UNKNOWN_ERROR, "新增用户失败");
        }
    }

    @RequestMapping(path = "/delete/id",method = RequestMethod.GET)
    public Response<String> deleteUser(@PathParam("id") int id){
        int deleteRes = userService.deleteUserById(id);
        if (deleteRes > 0){
            return Response.success("删除id为" + id + "的用户成功");
        }else {
            return Response.failure(ErrorCode.UNKNOWN_ERROR, "删除用户失败");
        }
    }

    @RequestMapping(path = "/delete/name",method = RequestMethod.GET)
    public Response<String> deleteUserName(String name){
        int deleteRes = userService.deleteUserByName(name);
        if (deleteRes > 0){
            return Response.success("删除姓名为" + name + "的用户成功");
        }else {
            return Response.failure(ErrorCode.UNKNOWN_ERROR, "删除用户失败");
        }
    }
    @RequestMapping(path = "/delete/time",method = RequestMethod.GET)
    public Response<String> deleteUserTime(String time) throws ParseException {
        int deleteRes = userService.deleteUserByTime(time);
        if (deleteRes > 0){
            return Response.success("删除创建时间在" + time + "之后的用户成功");
        }else {
            return Response.failure(ErrorCode.UNKNOWN_ERROR, "删除用户失败");
        }
    }


    @RequestMapping(path = "/update",method = RequestMethod.POST)
    public Response<User> updateUser( int id, String name, String description){
        User user = userService.updateUserById(id, name, description);
        return Response.success(userService.selectUserById(id));
    }

    @RequestMapping(path = "select/id/{id}",method = RequestMethod.GET)
    public Response<User> selectUserId(@PathVariable("id") int id){
        User user = userService.selectUserById(id);
        if (user != null){
            return Response.success(user);
        }else {
            return Response.failure(ErrorCode.UNKNOWN_ERROR,"用户不存在");
        }
    }

    @RequestMapping(path = "select/name",method = RequestMethod.GET)
    public Response<List<User>> selectUserName(String name){
        List<User> users = userService.selectUserByName(name);
        if (users.size() != 0){
            return Response.success(users);
        }else {
            return Response.failure(ErrorCode.UNKNOWN_ERROR,"符合条件用户不存在");
        }
    }

    @RequestMapping(path = "select/word",method = RequestMethod.GET)
    public Response<List<User>> selectUserWord(String word){
        List<User> users = userService.selectUserByWord(word);
        if (users.size() != 0){
            return Response.success(users);
        }else {
            return Response.failure(ErrorCode.UNKNOWN_ERROR,"符合条件用户不存在");
        }
    }

    @RequestMapping(path = "select/time",method = RequestMethod.GET)
    public Response<List<User>> selectUserTime(String begin, String end) throws ParseException {
        List<User> users = userService.selectUserByTime(begin, end);
        if (users.size() != 0){
            return Response.success(users);
        }else {
            return Response.failure(ErrorCode.UNKNOWN_ERROR,"符合条件用户不存在");
        }
    }

    @GetMapping("/limit/test")
    public void test(HttpServletResponse response) throws IOException {
        List<User> lists = new ArrayList<>();
        for (int i=0;i<5;i++){
            User user = new User();
            user.setId(i+1);
            user.setSex(new Random().nextInt(1));
            user.setName("张三" + i);
            user.setCreateTime(new Date());
//            user.setDescription("lalaal");
            lists.add(user);
        }
        String fileName = "test.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), User.class).sheet("sheet").doWrite(lists);
    }

    @GetMapping("test")
    public String test(){
        return "hello";
    }
}
